package com.meeting.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meeting.entity.Notice;
import com.meeting.mapper.NoticeMapper;
import com.meeting.service.NoticeService;
import com.meeting.vo.Result;
import org.springframework.stereotype.Service;

@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {

    @Override
    public Result getAllNotices(Integer page, Integer pageSize) {
        Page<Notice> pageObj = new Page<>(page, pageSize);
        LambdaQueryWrapper<Notice> wrapper = new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Notice::getTime);

        Page<Notice> result = this.page(pageObj, wrapper);
        return Result.success(result);
    }

    @Override
    public Result getNoticeById(Integer id) {
        Notice notice = this.getById(id);
        if (notice == null) {
            return Result.error("公告不存在");
        }
        return Result.success(notice);
    }

    @Override
    public Result saveNotice(Notice notice) {
        boolean saved = this.save(notice);
        if (saved) {
            return Result.success("公告发布成功");
        }
        return Result.error("公告发布失败");
    }

    @Override
    public Result updateNotice(Notice notice) {
        boolean updated = this.updateById(notice);
        if (updated) {
            return Result.success("公告更新成功");
        }
        return Result.error("公告更新失败");
    }

    @Override
    public Result deleteNotice(Integer id) {
        boolean deleted = this.removeById(id);
        if (deleted) {
            return Result.success("公告删除成功");
        }
        return Result.error("公告删除失败");
    }
}